<?php

class category extends BACKEND_Controller {
    /*
      Project     : 48c6c450f1a4a0cc53d9585dc0fee742
      Created on  : Mar 16, 2013, 11:29:15 PM
      Author      : Truong Khuong - khuongxuantruong@gmail.com
      Description :
      Purpose of the stylesheet follows.
     */

    function __construct() {
        parent::__construct();
		$this->types=array(
			'news'=>'Tin Tức',
			'product'=>'Sản phẩm',
			'customer'=>'Khách hàng',
			'thread'=>'Bài viết',
			'system'=>'Hệ thống'
		);
        $this->load->model('backend/category_model');
		$type=isset($_GET['type'])?$_GET['type']:null;
        $this->category_model = new category_model($type,true);
        $this->smarty = new CI_Smarty3();
        $this->smarty->error_reporting = E_ALL & ~E_NOTICE;
    }

    function index() {
        //print_r($this->category_model->HieCats);return;
        $assigns['categories'] = $this->category_model->HieCats;
		$assigns['types']=$this->types;
        $this->smarty
                ->assign($assigns)
                ->display("backend/03_category/01_list");
    }

    function editor($id = "") {
        $assigns = null;
        if ($id !== "")
            $assigns['item'] = $this->category_model->get($id);
        $assigns['categories'] = $this->category_model->HieCats;
		$assigns['types']=$this->types;
        $this->smarty
                ->assign($assigns)
                ->display("backend/03_category/02_editor");
    }

    function save() {
        $R["result"] = -1;
        $R["message"] = ascii_to_entities("Access is denied or Data is invalid.");
        $params=$this->input->post();
//        goto result;
//        $params["cate_title"] = $this->input->post("cate_title");
//        $params["cate_alias"] = $this->input->post("cate_alias");
        $params["cate_parent"] = (int) $params["cate_parent"];
        $params["cate_position"] = (int) $params["cate_position"];
//        $params["cate_status"] = $this->input->post("cate_status");
//        $params["cate_type"] = $this->input->post("cate_type");
        //$params["cat_level"] = 2;
        $cate_id = (int)$params["cate_id"];unset($params["cate_id"]);
        if ($params["cate_parent"] != 0 && $cate_id !=0) {
            if (in_array($cate_id, $this->category_model->HieCats[$params["cate_parent"]]->parents)){
                $R["message"] = 'Lỗi cấu trúc.';
                goto result;
            }
        }
//        foreach ($params as $tmp) {
//            if ($tmp === false or $tmp === "")
//                goto result;
//        }
        if ($cate_id != 0) {
            $rs = @$this->category_model->update($cate_id, $params);
        } else {
            $rs = @$this->category_model->insert($params);
        }
        if ($rs === true) {
            $R["result"] = 1;
            $R["message"] = ascii_to_entities("Dữ liệu đã được hệ thống ghi nhận.");
        } else {
            $R["result"] = -1;
            $R["error_number"] = $this->db->_error_number();
            $R["error_message"] = $this->db->_error_message();
            $R["message"] = ascii_to_entities("
                    Dữ liệu chưa được hệ thống ghi nhận.<br/>
                    Vui lòng kiểm tra dữ liệu và thử lại.<br/>
                    ");
            if ($R["error_number"] != 0)
                $R["message"].=ascii_to_entities("<span class='erc'>[" . $R["error_number"] . "] " . $R["error_message"] . ".</span>");
        }
        result:
        echo json_encode($R);
    }

    function test_proc() {
        print_r($this->category_model->proc_one());
    }

}

?>